<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>注册 - 智慧后勤综合管理系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body {
            background: linear-gradient(135deg, #e0f7fa, #bbdefb, #90caf9);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            font-family: 'Segoe UI', sans-serif;
        }
        .register-container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }
        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #2563eb;
        }
        input[type="text"], input[type="email"], input[type="password"] {
            width: 100%;
            padding: 12px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 6px;
        }
        button {
            width: 100%;
            padding: 12px;
            background: #2563eb;
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background: #1d4ed8;
        }
        p.error {
            color: red;
            text-align: center;
            margin-top: 10px;
        }
    </style>
</head>
<body>
<div class="register-container">
    <h2><i class="fas fa-user-plus"></i> 注册账号</h2>
    <input type="text" id="username" placeholder="请输入用户名" required>
    <input type="email" id="email" placeholder="请输入邮箱" required>
    <input type="password" id="password" placeholder="请输入密码" required>
    <button onclick="register()">注册</button>
    <p class="error" id="error-message"></p>
</div>

<script>
    function register() {
        const username = document.getElementById('username').value.trim();
        const email = document.getElementById('email').value.trim();
        const password = document.getElementById('password').value;

        if (!username || !email || !password) {
            document.getElementById('error-message').textContent = "请填写所有字段";
            return;
        }

        fetch('http://localhost:8080/api/register', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ username, email, password })
        })
            .then(res => res.json())
            .then(data => {
                if (data.success) {
                    alert("注册成功，请登录！");
                    window.location.href = 'login.html';
                } else {
                    document.getElementById('error-message').textContent = data.message || "注册失败";
                }
            })
            .catch(err => {
                console.error(err);
                document.getElementById('error-message').textContent = "网络错误，请稍后再试";
            });
    }
</script>
</body>
</html>
